<?php
namespace App\Controllers\Message;

use App\Models\User\User;
use App\Libs\Views\Json;
use App\Libs\Base\Constant;
use App\Models\Message\Message;
class GetMessageList extends \App\Libs\Base\BaseAction
{

    protected $type = ['expand','class','system'];
    public function execute()
    {
        $user = $this->session->user??[];
        if(empty($user)){
            return Json::view(Constant::UNLOGIN,'请登录',NULL);
        }
        if(!$this->request->hasQuery('type')){
            return Json::view(1,'缺少参数',NULL);
        }
        $type = $this->request->getQuery('type');
        if(!in_array($type,$this->type)){
            return Json::view(1,'查找类型不存在',NULL);
        }
        $page = $this->request->getQuery('page','int',1);
        $size = $this->request->getQuery('size','int',10);
        $offset = ($page-1)*$size;
        switch ($type){
            case 'expand':
                $searchType = 1;
                break;
            case 'class' :
                $searchType = 2;
                $allMsg = Message::findAll([
                    'uid=?1 and type=?2',
                    'bind' => [1=>$user->id,2=>$searchType],
                    'order' => 'hread asc,ctime desc',
                    'offset' => $offset,
                    'limit' => $size
                ]);
                $finalMsgData = [];
                $allMsg->rewind();
                if($allMsg->valid()){
                    while($allMsg->valid()){
                            $currentMsg = $allMsg->current();
                            $currentMsgData = $currentMsg->toArray(['id','uid','hread','type','subtype','title','content','ctime']);

                            $MsgData = json_decode($currentMsg->data,TRUE);
                            if(!isset($MsgData['className']) || !isset($MsgData['classScore'])){
                                $currentMsgData['className'] = '未知';
                                $currentMsgData['classScore'] = '未知';
                            }else{
                                $currentMsgData['className'] = $MsgData['className'];
                                $currentMsgData['classScore'] = $MsgData['classScore'];
                            }
                            $finalMsgData[] = $currentMsgData;
                            $allMsg->next();
                    }

                    foreach($finalMsgData as $key => $val){
                        $msgid[] = $val['id'];
                    }
                    //更你状态
                    $msgidStr = '('.implode(',',$msgid).')';
                    $changeStatus = $this->di['modelsManager']->createQuery("update App\Models\Message\Message set hread='1' where id IN ".$msgidStr);
                    $changeStatus->execute();
                    return Json::view(0,'ok',$finalMsgData);
                }else{
                    return Json::view(0,'ok',[]);
                }
                break;
            case 'system' :
                $searchType = 3;
                break;
        }
        $allMsg = Message::findAll([
            'uid=?1 and type=?2',
            'bind' => [1=>$user->id,2=>$searchType],
            'columns' => 'id,uid,hread,type,subtype,title,content,ctime',
            'order' => 'hread asc,ctime desc',
            'offset' => $offset,
            'limit' => $size
        ])->toArray();
        if(empty($allMsg)){
            return Json::view(0,'ok',[]);
        }else{

            foreach($allMsg as $key => $val){
                $msgid[] = $val['id'];
            }
            $msgidStr = '('.implode(',',$msgid).')';
            $changeStatus = $this->di['modelsManager']->createQuery("update App\Models\Message\Message set hread='1' where id IN ".$msgidStr);
            $changeStatus->execute();

            return Json::view(0,'ok',$allMsg);
        }
    }



}
